import {Injectable} from '@angular/core';
import {SkillCertificate} from '../types/resp/SkillCertificate';
import {SkillCertificateForm} from '../types/req/SkillCertificateForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {SkillCertificateQueryForm} from '../types/req/SkillCertificateQueryForm';

@Injectable({
  providedIn: 'root'
})
export class SkillCertificateService {
  skillCertificates: SkillCertificate[] = [
    {"id":1,"candidateId":3,"candidateName":"张伟","certificateName":"PMP项目管理专业人士认证","issuingAuthority":"PMI（项目管理协会）","issueDate":"2020-05-15","expiryDate":"2023-05-15","createTime":"2024-02-27 22:24:15","modifyTime":"2024-03-23 22:24:15"},{"id":2,"candidateId":4,"candidateName":"陈静","certificateName":"Scrum Master认证","issuingAuthority":"Scrum Alliance","issueDate":"2019-08-10","expiryDate":"2022-08-10","createTime":"2024-10-04 22:24:15","modifyTime":"2024-07-21 22:24:15"},
    {"id":3,"candidateId":2,"candidateName":"陈静","certificateName":"AWS解决方案架构师认证","issuingAuthority":"亚马逊云科技（AWS）","issueDate":"2021-03-20","expiryDate":"2024-03-20","createTime":"2024-08-17 22:24:15","modifyTime":"2024-01-16 22:24:15"},
    {"id":4,"candidateId":3,"candidateName":"张伟","certificateName":"Oracle认证数据库专家","issuingAuthority":"Oracle公司","issueDate":"2018-11-01","expiryDate":"2021-11-01","createTime":"2024-02-07 22:24:15","modifyTime":"2024-06-28 22:24:15"},
    {"id":5,"candidateId":2,"candidateName":"陈静","certificateName":"Google数据分析师认证","issuingAuthority":"Google","issueDate":"2022-06-05","expiryDate":"2025-06-05","createTime":"2024-01-15 22:24:15","modifyTime":"2024-10-19 22:24:15"},
    {"id":6,"candidateId":1,"candidateName":"王强","certificateName":"Cisco认证网络工程师","issuingAuthority":"Cisco","issueDate":"2017-09-12","expiryDate":"2020-09-12","createTime":"2024-02-15 22:24:15","modifyTime":"2024-04-11 22:24:15"},
    {"id":7,"candidateId":3,"candidateName":"张伟","certificateName":"微软Azure解决方案架构师认证","issuingAuthority":"微软","issueDate":"2023-02-10","expiryDate":"2026-02-10","createTime":"2024-12-08 22:24:15","modifyTime":"2024-06-21 22:24:15"}
  ]
  constructor() {
  }

  save (skillCertificateForm: SkillCertificateForm): Observable<Result<void>> {
    console.log('skillCertificateApi save params', skillCertificateForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (skillCertificateQueryForm: SkillCertificateQueryForm): Observable<Result<Page<SkillCertificate>>> {
    console.log('skillCertificateApi find params', skillCertificateQueryForm)
    let filteredSkillCertificates = this.skillCertificates
    if (skillCertificateQueryForm.candidateId) {
      filteredSkillCertificates = this.skillCertificates.filter(skillCertificate => skillCertificate.candidateId === skillCertificateQueryForm.candidateId);
    }
    return MockApi.queryPageSuccessfully(filteredSkillCertificates).pipe(
      map((response: Response<Result<Page<SkillCertificate>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<SkillCertificate>> {
    console.log('skillCertificateApi findById id', id)
    let target = {}
    for (const a of this.skillCertificates) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<SkillCertificate>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('skillCertificateApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
